
/*
 * HTTP请求的封闭类.
 */
package com.depth.viewer.request.http
{

	import com.depth.viewer.request.HttpPoolUtil;
	import com.depth.viewer.utils.Util;
	
	import mx.controls.Alert;
	import mx.messaging.messages.HTTPRequestMessage;
	import mx.rpc.AsyncToken;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;

	/**
	 *@private
	 * @author Administrator
	 *
	 */
	public class HttpRequestUtil
	{
		private var Service:HTTPService;
		public var proxy:String;
		
		private var _resultHandle:Function;
		private var _faultHandle:Function;
		private var _data:Object
		
		public function HttpRequestUtil(obj:Object = null)
		{
			Service=new HTTPService();
			Service.addEventListener(FaultEvent.FAULT,requestFaultHandle);
			Service.addEventListener(ResultEvent.RESULT,requestResultHandle);
			_data = obj;
		}
		public function requestFaultHandle(e:FaultEvent):void{
			trace("服务器连接出错！  " + Service.url);
			//Alert.show(Service.url+"\n"+e.message,"服务器连接失败");
			if (_faultHandle != null)
			{
				_faultHandle(e.message);
			}
		}
		public function requestResultHandle(e:ResultEvent):void{
			HttpPoolUtil.instance.sumOfError = 0;
			if(_resultHandle != null&&_data == null){
				_resultHandle(e.result);
			}else if(_resultHandle != null&&_data != null){
				_resultHandle(e.result,_data);
			}
		}
		public function httpPost(url:String, data:Object, mResult:Function=null, mFaule:Function=null, contentType:String=HttpParam.CONTENT_TYPE_FORM, resultFormat:String=HttpParam.RESULT_FORMAT_TEXT):AsyncToken
		{

			Service.contentType=contentType;
			Service.resultFormat=resultFormat;
			Service.method=HTTPRequestMessage.POST_METHOD;
			return createRequest(url, mResult, data, mFaule);
		}

		public function httpGet(url:String, data:Object, mResult:Function=null, mFaule:Function=null, resultFormat:String=HttpParam.RESULT_FORMAT_TEXT):AsyncToken
		{
			Service.resultFormat=resultFormat;
			Service.method=HTTPRequestMessage.GET_METHOD;
			return	createRequest(url, mResult, data, mFaule);
		}

		public function httpPut(url:String, data:Object, contentType:String=HttpParam.CONTENT_TYPE_FORM, resultFormat:String=HttpParam.RESULT_FORMAT_E4X, mResult:Function=null, mFaule:Function=null):AsyncToken
		{
			if (data)
			{
				data.method=HTTPRequestMessage.PUT_METHOD;
			}
			Service.contentType=contentType;
			Service.resultFormat=resultFormat;
			Service.method=HTTPRequestMessage.POST_METHOD;
			return createRequest(url, mResult, data, mFaule);
		}

		public function httpDelete(url:String, data:Object, contentType:String=HttpParam.CONTENT_TYPE_FORM, resultFormat:String=HttpParam.RESULT_FORMAT_E4X, mResult:Function=null, mFaule:Function=null):AsyncToken
		{
			if (data)
			{
				data.method=HTTPRequestMessage.DELETE_METHOD;
			}
			Service.contentType=contentType;
			Service.resultFormat=resultFormat;
			Service.method=HTTPRequestMessage.POST_METHOD;
			return createRequest(url, mResult, data, mFaule);
		}

		private function getParamXmlString(data:Object):String
		{
			var doc:String="<Params>";

			var keys:Array=Util.getObjectAttributes(data);
			for each (var param:Object in keys)
			{
				doc+="<Param name='" + param + "'>" + data[param] + " </Param>";
			}
			doc+="</Params>"
			return doc;
		}
        public function destroy():void{
			
		}
		public function createRequest(url:String, mResult:Function, data:Object=null, mFaule:Function=null):AsyncToken
		{
			var params:Object=null;
			if (this.proxy)
			{
				params={};
				params["params"]=getParamXmlString(data);
				params["url"]=url;
				params["method"]=Service.method;
				url=this.proxy;
			}
			else
			{
				params=data;
				/*if (url.indexOf("?") == -1 && url.indexOf("&") == -1)
				{
					url+="?date=" + new Date().time;
				}
				else if (url.indexOf("?") != -1)
				{
					url+="&date=" + new Date().time;

				}*/
			}
			Service.url=url;
			_resultHandle = mResult;
			_faultHandle = mFaule;
			
			var token:AsyncToken;
			if (params)
			{
				token = Service.send(params);
			}
			else
			{
				token = Service.send();
			}
			return token;
		}


	}
}